package org.dtrd.modules.shop.service.impl;

import org.dtrd.modules.shop.entity.po.DtrdEntShcRdOrderJoinAddress;
import org.dtrd.modules.shop.mapper.DtrdEntShcRdOrderJoinAddressMapper;
import org.dtrd.modules.shop.service.IDtrdEntShcRdOrderJoinAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.shop.entity.bean.OrderJoinAddressDetail;
import org.dtrd.modules.shop.entity.bean.OrderJoinAddressListInfo;
import org.dtrd.modules.shop.entity.bean.OrderJoinAddressListRequest;
import org.dtrd.modules.shop.entity.bean.OrderJoinAddressPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 订单与收货地址关联关系 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-07-23
 */
@DS("multi-datasource1")
@Service
public class DtrdEntShcRdOrderJoinAddressServiceImpl extends ServiceImpl<DtrdEntShcRdOrderJoinAddressMapper, DtrdEntShcRdOrderJoinAddress> implements IDtrdEntShcRdOrderJoinAddressService {

    private DtrdEntShcRdOrderJoinAddressMapper dtrdEntShcRdOrderJoinAddressMapper;

    @Override
    public List<OrderJoinAddressListInfo> getOrderJoinAddressList(OrderJoinAddressListRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdOrderJoinAddress> queryWrapper = baseQueryWrapper();
        List<DtrdEntShcRdOrderJoinAddress> poList = list(queryWrapper);
        List<OrderJoinAddressListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(OrderJoinAddressListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<OrderJoinAddressListInfo> pageOrderJoinAddress(OrderJoinAddressPageRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdOrderJoinAddress> queryWrapper = baseQueryWrapper();
        IPage<DtrdEntShcRdOrderJoinAddress> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(OrderJoinAddressListInfo::new);
    }

    @Override
    public OrderJoinAddressDetail getOrderJoinAddress(Integer dataId) {
        DtrdEntShcRdOrderJoinAddress dtrdEntShcRdOrderJoinAddress = getById(dataId);
        return Optional.ofNullable(dtrdEntShcRdOrderJoinAddress)
                        .map(po -> new OrderJoinAddressDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public OrderJoinAddressDetail getOrderJoinAddressByOrderId(String orderId) {
        LambdaQueryWrapper<DtrdEntShcRdOrderJoinAddress> queryWrapper = baseQueryWrapper();
        queryWrapper.eq(DtrdEntShcRdOrderJoinAddress::getOrderId, orderId);

        DtrdEntShcRdOrderJoinAddress dtrdEntShcRdOrderJoinAddress = getOne(queryWrapper);
        return Optional.ofNullable(dtrdEntShcRdOrderJoinAddress)
                .map(po -> new OrderJoinAddressDetail().parseFromPo(po))
                .orElse(null);
    }

    @Override
    public boolean saveOrUpdateOrderJoinAddress(OrderJoinAddressDetail dto) {
        DtrdEntShcRdOrderJoinAddress dtrdEntShcRdOrderJoinAddress = new DtrdEntShcRdOrderJoinAddress();
        if(dto.getDataId() == null){
            dtrdEntShcRdOrderJoinAddress = dtrdEntShcRdOrderJoinAddress.init();
        }
        dtrdEntShcRdOrderJoinAddress.parseFromDto(dto);
        return saveOrUpdate(dtrdEntShcRdOrderJoinAddress);
    }

    @Override
    public boolean removeOrderJoinAddress(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntShcRdOrderJoinAddress> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntShcRdOrderJoinAddress.class)
                        .eq(DtrdEntShcRdOrderJoinAddress::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntShcRdOrderJoinAddress> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntShcRdOrderJoinAddress.class)
                        .eq(DtrdEntShcRdOrderJoinAddress::getIsDel, 0);
    }
}
